<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<script type="text/javascript">
   $(function(){
   	 xf.ns('xf.admin.menu${OP.menuId}');
   	 $('#admin_menu${OP.menuId}_datagrid').bootstrapTable({ 
          url: rootPath + '/sysUser/selectSysUserList.do', 
          height: '100%',
          sortName: 'userId',
          sortOrder: 'desc',
          striped: true,
          pagination: true,
          pageSize: 10,
          pageList: [10, 100, 200],
          search: false,
          sidePagination:'server',
          idField: 'id',
          uniqueId: 'id',
          responseHandler: responseHandler,
          queryParams: queryParams,
          minimumCountColumns: 2,
          clickToSelect: true,
          searchAlign: 'left',
          columns: [{
              checkbox: true
          }, {
              field: 'userId',
              title: '编号',
              visible : false
          }, {
              field: 'loginUser',
              title: '账号',
              align: 'center',
              valign: 'middle'
          }, {
              field: 'userName',
              title: '姓名',
              align: 'center',
              valign: 'middle'
          },{
              field: 'roleName',
              title: '角色名称',
              align: 'center',
              valign: 'middle'
          },{
              field: 'createDate',
              title: '创建日期',
              align: 'center',
              valign: 'middle'
          },{
              field: 'createTime',
              title: '创建时间',
              align: 'center',
              valign: 'middle'
          },{
              field: 'status',
              title: '状态',
              align: 'center',
              valign: 'middle'
          },{
              field: 'operate',
              title: '操作',
              align: 'center',
              formatter: operateFormatter,
              events:{
           	      'click .roleSet': function (e, value, row, index) {
           	    	roleSet(row.userId);
           	      },
              }
          }]
    });
})
    
function stateFormatter(value, row, index) {
    if (value == 1) {
    	return '<i class="fa fa-check text-navy"></i>';
    }else{
    	return '<i class="fa fa-close text-danger"></i>';
    }
    return value;
}

// 传递的参数
function queryParams(params) {
	var pageSize = params.limit;
	var sort = params.sort;
	var offset = params.offset;
	var order = params.order;
	var pageNum = offset / pageSize + 1;
	var temp={
   		pageSize : pageSize,
   		pageNum : pageNum,
   		sort : sort,
   		order : order
    }
    var data=serializeForm("admin_menu${OP.menuId}_formSearch");
    $.extend(temp,data);
    return temp;
}

//查询
xf.admin.menu${OP.menuId}.serach = function(){
    $('#admin_menu${OP.menuId}_datagrid').bootstrapTable('refresh');
}

//清空查询条件输入域
xf.admin.menu${OP.menuId}.clearForm=function(){
	$("#roleId").selectPageClear();
    $("#admin_menu${OP.menuId}_formSearch")[0].reset();
}

//新增
xf.admin.menu${OP.menuId}.add = function(){
// 	var add=${OP.menuId}+"add";
// 	$("#"+add).attr("disabled", "true");
	xf.showWindow({
			title:'增加管理员',
			href:rootPath + '/op_edit_${OP.menuId}.do',
			width:'50%',
			height:'40%',
			okhandler:function(){
				xf.admin.menu${OP.menuId}.save();
			},
			cancelhandler:function(){ 
				xf.closeWindow();
			}
		});
	return false;
}

//编辑
xf.admin.menu${OP.menuId}.edit = function(){
	var rows =$('#admin_menu${OP.menuId}_datagrid').bootstrapTable('getSelections');
	if(rows.length==0){
		xf.toastrsAlert({
		     code:'info',
		     msg:'请选择你要编辑的记录'
		});
		return;
	}
	if(rows.length > 1){
		xf.toastrsAlert({
		     code:'warning',
		     msg:'sorry , 只能选择一条进行编辑'
		});
		return;
	}
	xf.showWindow({
			title:'编辑管理员',
			href:rootPath + '/op_edit_${OP.menuId}.do?userId='+rows[0].userId,
			width:'50%',
			height:'40%',
			okhandler:function(){
				xf.admin.menu${OP.menuId}.update();
			},
			cancelhandler:function(){
				xf.closeWindow();
			}
		});
}

//删除
xf.admin.menu${OP.menuId}.del = function(){ 
	var rows =$('#admin_menu${OP.menuId}_datagrid').bootstrapTable('getSelections');
	if(rows.length==0){
		xf.toastrsAlert({
		     code:'info',
		     msg:'请选择你要删除的记录'
		});
		return;
	}
	xf.confirm(function(){
		var ps = [];
    	$.each(rows,function(i,n){
    		ps.push(n.userId);
    	});
    	$.ajax({
            type: 'post',
            url: rootPath + '/op_remove_${OP.menuId}.do',
            data: {"ids":ps.join(",")},
            dataType: 'json',
            success: function (data) {
            	$('#admin_menu${OP.menuId}_datagrid').bootstrapTable('refresh');
            	xf.toastrsAlert({
              		 code: data.success ? 'success' :'error',
    		       	 msg: data.success ? '成功' :'失败'
         		});
            }
        });
	});
}

var selectData=selectRole();

//角色下拉列表
function selectRole(){
	$.ajax({
        type: 'post',
        url: rootPath + '/sysRole/selectSysRole.do',
        data: '',
        dataType: 'json',
        success: function (data) {
        	 $('#roleId').selectPage({
        	     showField : 'roleName',
        	     keyField : 'roleId',
        	     data : data
        	 });
        }
    });
}

//操作
function operateFormatter(value, row, index) {
	return '<a class="roleSet" href="javascript:void(0)" title="设置角色"><i class ="fa fa-wrench" aria-hidden="true"></i></</a>';
}

//设置角色
function roleSet(userId){
	xf.showWindow({
		title:'设置角色',
		href:rootPath + '/op_roleTree_${OP.menuId}.do?userId='+userId,
		width:'50%',
		height:'40%',
		okhandler:function(){
			xf.admin.menu${OP.menuId}.update();
		},
		cancelhandler:function(){
			xf.closeWindow();
		}
	}); 
}

</script>

<div class="wrapper wrapper-content animated fadeInRight">
	<div class="ibox float-e-margins">
		<div class="ibox-content">
			<div class="panel panel-default">
			<div class="panel-heading">查询条件</div>
					<div class="panel-body">
						<form id="admin_menu${OP.menuId}_formSearch" class="form-horizontal">
							<div class="form-group" style="margin-top: 15px">
								<label class="control-label col-sm-1" for="txt_search_departmentname">账号</label>
								<div class="col-sm-2">
									<input type="text" class="form-control" name="loginUser" id="loginUser">
								</div>
								<label class="control-label col-sm-1" for="txt_search_departmentname">姓名</label>
								<div class="col-sm-2">
									<input type="text" class="form-control" name="userName" id="userName">
								</div>
								<label class="control-label col-sm-1" for="txt_search_departmentname">角色</label>
								<div class="col-sm-2">
<!-- 								    <select id="roleId" name="roleId" class="form-control"></select> -->
									<input type="text" class="form-control" name="roleId" id="roleId">
								</div>
								<div class="col-sm-12" style="text-align: right;">
									<button type="button"  onclick = "xf.admin.menu${OP.menuId}.serach();"
										class="btn btn-primary"><i class="fa fa-search"></i>查询</button>
								    <button type="button" class="btn btn-white" onclick="xf.admin.menu${OP.menuId}.clearForm();"><i class="fa fa-eraser"></i>重置</button>
								</div>
							</div>
						</form>
					</div>
				</div>
			
			<%@ include file="/WEB-INF/views/common/toolbar.jsp"%>
			<div class="panel-body" style="padding:0;">
				<div class="panel panel-default">
					<div class="panel-heading">列表</div>
					<div class="panel-body">
						<table id="admin_menu${OP.menuId}_datagrid" data-toolbar="#toolbar"
							data-show-refresh="true" data-show-toggle="true"
							data-show-columns="true" data-show-footer="false"
							data-mobile-responsive="true">
						</table>
					</div>
				</div>
			</div>
<!-- 			<div class="table-responsive"> -->
<%-- 				<table id="admin_menu${OP.menuId}_datagrid" data-toolbar="#toolbar" --%>
<!-- 					data-show-refresh="true" data-show-toggle="true" -->
<!-- 					data-show-columns="true" data-show-footer="false" -->
<!-- 					data-mobile-responsive="true"> -->
<!-- 				</table> -->
<!-- 			</div> -->
		</div>
	</div>
</div>